﻿@{
    Layout = "~/Areas/DM/Views/Shared/_Layout.cshtml";
}

@section Scripts{

    <script type="text/javascript">
        $.extend($.fn.validatebox.defaults.rules, {
            /*必须和某个字段相等*/
            equalTo: {
                validator: function (value, param) {
                    return $(param[0]).val() == value;
                },
                message: '字段不匹配'
            }

        });
        //初始化
        $(function () {
            Init();
            bindGrid();
            QuanXian();
        });
        //页面动态改动
        $(window).resize(function () {
            $.setGridWithSearchBar("#TableForm", "#SearchBar", "#tbList");
        });
        function bindGrid() {
            $("#tbList").datagrid({
                width: 'auto',
                height: 'auto',
                fitColumns: true,
                striped: true,
                singleSelect: true,
                url: '@Url.Content("~/DM/User/Index")',
                loadMsg: '数据加载中请稍后……',
                pagination: true,//启用分页，默认每页10行
                rownumbers: true,//显示页码，默认 提供 10 - 50 的页容量选择下拉框
                pageSize: 12,//设置 页容量为 5
                pageList: [12, 24, 36],//设置 页容量下拉框
                columns: [[
                      { field: 'DepName', title: '部门', width: 50 },
                    { field: 'WorkCode', title: '工号', width: 20 },
                    { field: 'LoginName', title: '登录名', width: 30 },
                    { field: 'Name', title: '姓名', width: 70 },
                    { field: 'Gender', title: '性别', width: 30 },
                    { field: 'SN', title: '顺序号', width: 20 },
                    {
                        field: 'IsActive', title: '是否启用', formatter: function (val) {
                            return val ? "是" : "否";
                        }
                    },
                    { //add2021-10-15 plq 医保上线移植
                        field: 'PhysicianCode', title: '医师编号', width: 30
                    },
                    {
                        field: 'YBCountryCode', title: '医师国家码', width: 30
                    },

                    { title: 'ID', field: 'ID', sortable: false, hidden: true },
                { title: 'DepID', field: 'DepID', sortable: false, hidden: true }
                ]],
                rowStyler: function (index, row) {
                    if (row.IsActive == false) {
                        return 'color:red';
                    }
                },
                queryParams: {
                    WorkCode: $("#WorkCode").val(),
                    Name: $("#Name").val(),
                    SearchBound: $("#SearchBound").val()
                },
                toolbar: [
                {
                    id: 'btnFenPei',
                    iconCls: 'icon-edit',
                    text: "分配部门角色",
                    handler: setDepRole
                }
                , '-', {
                    id: 'btnNew',
                    text: '新增',
                    iconCls: 'icon-add',
                    handler: function () {
                        $("#EID").val("");
                        $("#EDepID").combotree("setValue", "-1");
                        $("#ELoginName").textbox("setText", "");
                        $("#EPassWord").val("");
                        $("#ERePassWord").val("");
                        $("#EWorkCode").textbox("setText", "");
                        $("#EName").textbox("setText", "");
                        $('#EGender').combobox('setText', "男");
                        $("#ESN").numberbox("setValue", "255");
                        $("#PhysicianCode").textbox("setText", ""); //add2021-10-15 plq 医保上线移植
                        $("#YBCountryCode").textbox("setText", ""); //add2022-03-28 plq 医保国家码上线移植
                        $('#btnadd').show();
                        $('#btnadd').linkbutton('enable');
                        $('#btnedit').hide();
                        $('#trPwd').show();
                        $('#trPwdRepeat').show();
                        $('#NewUserList').dialog('open');
                    }
                }, '-',
                   {
                       id: 'btnUpdate',
                       text: '修改',
                       iconCls: 'icon-edit',
                       handler: function () {
                           var row = $('#tbList').datagrid('getSelected');
                           if (row) {
                               $("#EID").val(row.ID);
                               $("#EDepID").combotree('setValue', row.DepID);
                               $("#ELoginName").textbox("setText", row.LoginName);
                               $("#EWorkCode").textbox("setText", row.WorkCode);
                               $("#EName").textbox("setText", row.Name);
                               $('#EGender').combobox('setText', row.Gender);
                               $("#ESN").numberbox("setValue", row.SN);
                               $("#PhysicianCode").textbox("setText", row.PhysicianCode); //add2021-10-15 plq 医保上线移植
                               $("#YBCountryCode").textbox("setText", row.YBCountryCode); //add2022-03-28 plq 医保国家码上线移植
                               $('#btnadd').hide();
                               $('#btnedit').show();
                               $('#btnedit').linkbutton('enable');
                               $('#trPwd').hide();
                               $('#trPwdRepeat').hide();
                               $('#NewUserList').dialog('open');
                           } else {
                               $.messager.alert('提示', '请选择数据行', 'info', function () {
                               });
                           }
                       }
                   }, '-',
                   {
                       id: 'btnJinYong',
                       text: '<span style="color:red">禁用</span>',
                       iconCls: 'icon-custom-cJinYong',
                       handler: function () {
                           var row = $('#tbList').datagrid('getSelected');
                           if (row) {
                               UserDisable(false);
                           }
                           else {
                               $.messager.alert('提示', '请选择数据行', 'info', function () {
                               });
                           }
                       }
                   }, '-',
                   {
                       id: 'btnQiYong',
                       text: '<span style="color:darkgreen">启用</span>',
                       iconCls: 'icon-custom-cQiYong',
                       handler: function () {
                           var row = $('#tbList').datagrid('getSelected');
                           if (row) {
                               UserDisable(true);
                           }
                           else {
                               $.messager.alert('提示', '请选择数据行', 'info', function () {
                               });
                           }
                       }
                   }
                   , '-',
                   {
                       id: 'btnRePwd',
                       text: '<span style="color:orange">重置密码</span>',
                       iconCls: 'icon-redo',
                       handler: function () {
                           var row = $('#tbList').datagrid('getSelected');
                           if (row) {
                               $('#RID').val(row.ID);
                               $('#rePwdWindow').dialog('open');
                           }
                           else {
                               $.messager.alert('提示', '请选择数据行', 'info', function () {
                               });
                           }
                       }
                   }
                ],
                onSelect: function (index, row) {
                    if (row.IsActive) {
                        $('#btnQiYong').linkbutton('disable');
                        $('#btnJinYong').linkbutton('enable');
                    } else {
                        $('#btnQiYong').linkbutton('enable');
                        $('#btnJinYong').linkbutton('disable');
                    }
                    //selectedRowIndex = rowI;//将选中行的下标 设置给全局变量
                }
            });
            $.setGridWithSearchBar("#TableForm", "#SearchBar", "#tbList");

        }
    </script>
    <script type="text/javascript">
    function QuanXian() {
        if ('@ViewData["PersonInsert"]' == "1")
        { $('#btnNew').show(); }
        else
        { $('#btnNew').hide(); }
        if ('@ViewData["PersonEdit"]' == "1") {
            $('#btnUpdate').show();
        }
        else {
            $('#btnUpdate').hide();
        }
        if ('@ViewData["PersonDelete"]' == "1") {
            $('#btnJinYong').show();
            $('#btnQiYong').show();
        }
        else {
            $('#btnJinYong').hide();
            $('#btnQiYong').hide();
        }
        if ('@ViewData["PersonFenPei"]' == "1")
        { $('#btnFenPei').show(); }
        else
        { $('#btnFenPei').hide(); }
        if ('@ViewData["PersonUpdatePwd"]' == "1")
            { $('#btnRePwd').show(); }
            else
            { $('#btnRePwd').hide(); }

        }
        //查询
        function reSearch() {
            $("#tbList").datagrid({
                queryParams: {
                    WorkCode: $("#WorkCode").val(),
                    Name: $("#Name").val(),
                    SearchBound: $("#SearchBound").val()
                }
            });
            QuanXian();//20161121--增加点击查询按钮判断是否有权限
        }
        //保存密码
        function SavePwd() {
            $.post("@Url.Content("~/DM/User/RePassword")", { "uID": $('#RID').val(), "newPwd": $("#RPassWord").val() }, function (jsonData) {
            $('#rePwdWindow').dialog('close');
            $.handleJsonResult(jsonData, function () {
                //$("#tbList").datagrid("reload");// 刷新页面
            });
        });
    }
    //delete
    function UserDisable(boolDisable) {
        var rowData = $('#tbList').datagrid('getSelected');
        if (rowData) {
            var str;
            if (boolDisable)
                str = "确定将【" + rowData.Name + "】设置为“<span style='color:darkgreen'>有效</span>”吗？";
            else
                str = "确定将【" + rowData.Name + "】设置为“<span style='color:red'>无效</span>”吗？";
            $.messager.confirm('提示', str, function (r) {
                if (!r) {
                    return;
                }
                $.ajax({
                    type: 'POST',
                    url: '@Url.Content("~/DM/User/UserDisable")',
                    data:
                        {
                            id: rowData.ID,
                            disable: boolDisable
                        },
                    dataType: "json",
                    success: function (jsData) {
                        $.handleJsonResult(jsData, function () {
                            $("#tbList").datagrid("reload");
                        });
                    }
                });
            });
        }
        else {
            $.messager.alert('提示', '请选择数据行', 'info', function () { });
        }
    }



    //下拉绑定
    function Init() {
        $("#selDep").combotree({
            prompt: '--请选择--',
            url: '@Url.Content("~/DM/Department/GetAllDepart")',//ajax后台取数据路径，返回的是json格式的数据
            required: true,
            method: 'POST'
        });
        $("#EDepID").combotree({
            prompt: '--请选择--',
            url: '@Url.Content("~/DM/Department/GetAllDepart")',//ajax后台取数据路径，返回的是json格式的数据
            required: true,
            method: 'POST'
        });
        $('#selRole').combobox({
            prompt: '--请选择--',
            url: '@Url.Content("~/DM/Role/GetRoleByDepId/")',
            valueField: 'ID',
            textField: 'Name',
            method: 'POST',
            editable: false,
            required: true,
            multiple: true
        });

        //取带教人员 add2024-08-29 plq
        $.EUIcombobox("#selGroup", {
            url: '@Url.Content("~/DM/User/GetTeachUserDic/")',
            valueField: 'ID',
            textField: 'Name',
            editable: true,
            OneOption: [{
                ID: "",
                Name: "--请选择--"
            }]
        });
    }

    ////选中行的下标
    //var selectedRowIndex = -1;

    //当前选中的用户数据
    var usrData = null;
    //设置部门权限
    function setDepRole() {
        //获取选中行里的数据
        usrData = $("#tbList").datagrid("getSelected");
        if (usrData) {
            $("#selDep").combotree("clear");
            $('#selRole').combobox("clear");
            $('#selGroup').combobox("clear"); //分配的带教医生 add2024-08-29 plq
            $("#GWorkCode").val(usrData.WorkCode); //选中用户的工号 add2024-08-29 plq

            $.ajax({
                type: 'POST',
                url: '@Url.Content("~/DM/User/GetUserDepAndRole")',
                data:
                    {
                        UserId: usrData.ID

                    },
                dataType: "json",
                success: function (jsData) {
                    if (jsData.Dep != "") {
                        $("#selDep").combotree("setValue", jsData.Dep);
                    }
                    if (jsData.UserRole != "") {
                        $('#selRole').combobox("setValues", jsData.UserRole);


                    }
                }
            });

            //选择用户分配的带教医生(闵行移植) add2024-08-29 plq
            $.ajax({
                type: 'POST',
                url: '@Url.Content("~/DM/User/GetUserGroup")',
                data:
                    {
                        WorkCode: usrData.WorkCode
                    },
                dataType: "json",
                success: function (jsData) {
                    //console.log(jsData);
                    if (jsData.length > 0) {
                        $("#selGroup").combobox("setValue", jsData[0].WorkCode);
                    }
                }
            });

            $('#winDRoles').dialog('open');
        } else {
            $.messager.alert('提示', '请选择数据行', 'info', function () {
            });
        }
    }

    //保存用户角色（每个用户只有一个角色）
    function SaveRoles() {
        var nowDepId = $("#selDep").combotree("getValue");
        var arr = $("#selRole").combobox("getValues");
        var workcode = $("#GWorkCode").val(); //选中用户的工号 add2024-08-29 plq
        var pworkcode = $("#selGroup").combobox("getValue"); //分配的带教医生 add2024-08-29 plq
        var rIds = "";
        for (var i = 0; i < arr.length; i++) {
            rIds += arr[i] + ",";
        }
        if (!arr || arr.length == 0) return;     //?depid=1&rids=1,2,3
        $.post("@Url.Content("~/DM/Department/SetUserRole")", { "uID": usrData.ID, "depId": nowDepId, "rIds": rIds, "workcode": workcode, "pworkcode": pworkcode }, function (jsonData) {
            $('#winDRoles').dialog('close');

            $.handleJsonResult(jsonData, function () {
                $("#tbList").datagrid("reload");// 刷新页面
            });
        });
    }
    </script>
}


<div id="TableForm" class="easyui-layout" style="width:auto; height: auto; margin-left: auto; margin-right: auto">
    <div data-options="region:'center',title:'人员信息',iconCls:'icon-custom-cUser'">
        <div id="SearchBar" data-options="region:'north'" style="overflow: hidden; padding: 10px">
            <table>
                <tr>
                    <td>工号：</td>
                    <td>
                        <input id="WorkCode" class="easyui-textbox" style="width:150px" />
                    </td>
                    <td>姓名：</td>
                    <td>
                        <input id="Name" class="easyui-textbox" style="width:150px" />
                    </td>
                    <td rowspan="2">
                        <input id="SearchBound" type="hidden"  value='@ViewData["SearchBound"]'/>
                        <a href="javascript:void(0)" id="btnsearch" class="easyui-linkbutton" data-options="iconCls:'icon-search'" onclick="reSearch()">查询</a>
                    </td>
                </tr>
            </table>
        </div>

        <div data-options="region:'center'" style="background: #fafafa; overflow: hidden">
            <table id="tbList"></table>
        </div>

    </div>
</div>


<div id="winDRoles" class="easyui-dialog" title="设置部门角色" data-options="modal:true,collapsible:false,minimizable:false,maximizable:false,closed:true,resizable:false,iconCls:'icon-custom-Surplus'" style="width:350px;height:200px;padding:0px;">
    <div data-options="region:'center'" style="overflow: hidden">
        <input id="GWorkCode" type="hidden" />
        <table style="width:99%">
            <tr style="height:40px">
                <td>&nbsp;&nbsp;部门：</td>
                <td>
                    <input id="selDep" name="selDep" style="width:180px">
                </td>
            </tr>
            <tr style="height:40px">
                <td>&nbsp;&nbsp;角色：</td>
                <td>
                    <input id="selRole" name="selRole" style="width:180px">
                </td>
            </tr>
            <tr style="height:40px">
                <td>&nbsp;&nbsp;带教医生：</td>
                <td>
                    <input id="selGroup" name="selGroup" class="easyui-combobox" style="width: 180px" />
                </td>
            </tr>

            <tr style="height:30px">
                <td colspan="2" align="center">
                    <a href="javascript:void(0)" id="btnSave" class="easyui-linkbutton" data-options="iconCls:'icon-edit'" onclick="SaveRoles()">保存</a>
                </td>
            </tr>

        </table>
    </div>
</div>
<div id="rePwdWindow" class="easyui-dialog" title="重置密码" data-options="modal:true,collapsible:false,minimizable:false,maximizable:false,closed:true,resizable:false,iconCls:'icon-custom-Surplus'" style="width:300px;height:155px;padding:0px;">
    <div data-options="region:'center'" style="overflow: hidden">
        <table style="width:99%">
            <tr style="height:40px">
                <td>&nbsp;&nbsp;新密码：</td>
                <td>
                    <input type="password" id="RPassWord" class="easyui-validatebox" style="width:160px" data-options="required:true" />
                    <input id="RID" type="hidden" value='@ViewData["SearchBound"]' />
                </td>
            </tr>
            <tr style="height:40px">
                <td>&nbsp;&nbsp;重复密码：</td>
                <td>
                    <input type="password" id="RRePassWord" class="easyui-validatebox" style="width:160px" data-options="required:true"
                           validtype="equalTo['#RPassWord']" invalidmessage="两次输入密码不匹配" />
                </td>
            </tr>

            <tr style="height:30px">
                <td colspan="2" align="center">
                    <a href="javascript:void(0)" id="btnSavePwd" class="easyui-linkbutton" data-options="iconCls:'icon-edit'" onclick="SavePwd()">保存</a>
                </td>
            </tr>

        </table>
    </div>
</div>
@Html.Partial("~/Areas/DM/Views/User/UserEdit.cshtml")